//
//  ARTSleepSetPopView.m
//  ChartsDemo
//
//  Created by yunda on 2022/1/18.
//

#import "ARTSleepSetPopView.h"

@implementation ARTSleepSetModel

- (instancetype)initWithTitle:(NSString *)title value:(NSString *)value{
    self = [super init];
    if (!self) {
        return nil;
    }
    _title = title;
    _value = value;
    return self;
}

@end

@implementation ARTSleepSetPopView

-  (instancetype)init{
    self = [super init];
    if (!self) {
        return nil;
    }
    // init code
    // code
    NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"ARTSleepSetPopView" owner:nil options:nil];
    // 加载nib
    self = [arrayOfViews objectAtIndex:0];
    CGFloat x = 30;
    CGFloat w = J_SCREEN_WIDTH-x*2;
    CGFloat h = w*0.8;
    self.frame = CGRectMake(30, (J_SCREEN_HEIGHT-h)/2, w, h);
    J_ViewRadius(self, 10);
    
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    return self;
}

- (void)setDataSourceArray:(NSArray<ARTSleepSetModel *> *)dataSourceArray{
    _dataSourceArray = dataSourceArray;
    [_pickerView reloadAllComponents];
    for (int i = 0; i < dataSourceArray.count; i++) {
        ARTSleepSetModel *model = dataSourceArray[i];
        if ([@(_defaultValue).stringValue isEqualToString:model.value]) {
            [_pickerView selectRow:i inComponent:0 animated:YES];
            _currentModel = model;
            break;
        }
    }
}

- (IBAction)okAction:(id)sender {
    if (self.okBlock) {
        self.okBlock(_currentModel);
    }
    [_presentVc hideInView:[ARTNavigation currentNavigationViewController].view animated:YES completion:nil];
}

- (void)show{
    QMUIModalPresentationViewController *presentVc = [[QMUIModalPresentationViewController alloc]init];
    presentVc.contentView = self;
    presentVc.animationStyle = QMUIModalPresentationAnimationStyleSlide;
    presentVc.contentViewMargins = UIEdgeInsetsMake(0, 0, 0, 0 );
    presentVc.contentView.backgroundColor = [UIColor whiteColor];
    [presentVc showInView:[ARTNavigation currentNavigationViewController].view animated:YES completion:nil];
    _presentVc = presentVc;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataSourceArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 34;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    ARTSleepSetModel *model = self.dataSourceArray[row];
    return model.title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    ARTSleepSetModel *model = self.dataSourceArray[row];
    _currentModel = model;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end
